package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.pojo.MessageObject;
import cn.zj.logistics.pojo.Permission;
import cn.zj.logistics.pojo.PermissionExample;
import cn.zj.logistics.pojo.PermissionExample.Criteria;
import cn.zj.logistics.pojo.Role;
import cn.zj.logistics.pojo.RoleExample;
import cn.zj.logistics.service.PermissionService;
import cn.zj.logistics.service.RoleService;

@Controller
@RequestMapping("/permission")
public class PermissionController {

	@Autowired
	private PermissionService permissionService;
	
	@RequiresPermissions("permission:permissionPage")
	@RequestMapping("/permissionPage")
	public String permissionPage() {

		return "permissionPage";
	}

	@RequiresPermissions("permission:list")
	@RequestMapping("/list")
	@ResponseBody
	public <T> PageInfo<Permission> list(@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "10") int pageSize, String keyword) {

		PageHelper.startPage(pageNum, pageSize);

		PermissionExample example = new PermissionExample();

		// 前台传递关键字参数不为空的时候才参与条件查询
		if (StringUtils.isNotBlank(keyword)) {
			System.out.println("keyword :" + keyword);
			Criteria criteria1 = example.createCriteria();
			criteria1.andNameLike("%" + keyword + "%");
			
			Criteria criteria2 = example.createCriteria();
			criteria2.andParentNameLike("%" + keyword + "%");
			
			//OR关系
			example.or(criteria2);
			
		}

		List<Permission> permissions = permissionService.selectByExample(example);

		PageInfo<Permission> pageInfo = new PageInfo<>(permissions);

		return pageInfo;
	}

	
	@RequestMapping("/getAllPermission")
	@ResponseBody
	public List<Permission> getAllPermission(){
		
		PermissionExample example = new PermissionExample();
		List<Permission> permissions = permissionService.selectByExample(example );
		return permissions;
	}
	
	
	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject delete(Long permissionId) {
		
		//删除前判断当前权限是否是拥有子权限
		
		PermissionExample example = new PermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(permissionId);
		List<Permission> children = permissionService.selectByExample(example );
		
		if(children.size() >0) {
			
			return MessageObject.createMo(0, "此权限还有子权限，不能删除！");
		}
		

		int row = permissionService.deleteByPrimaryKey(permissionId);

		MessageObject mo = MessageObject.createMo(0, "删除数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "删除数据成功");
		}
		return mo;
	}

	// 新增功能
	@RequestMapping("/insert")
	@ResponseBody
	public MessageObject insert(Permission permission) {

		int row = permissionService.insert(permission);

		MessageObject mo = MessageObject.createMo(0, "新增数据数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "新增数据成功");
		}
		return mo;
	}

	// 新增功能
	@RequestMapping("/update")
	@ResponseBody
	public MessageObject update(Permission permission) {

		int row = permissionService.updateByPrimaryKeySelective(permission);

		MessageObject mo = MessageObject.createMo(0, "修改数据数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "修改数据成功");
		}
		return mo;
	}

	@RequestMapping("/permissionEdit")
	public String permissionEdit(Model m, Long permissionId) {


		if (permissionId != null) {
			// 查询出当前要修改的用户信息共享给编辑页面以供修改数据回显
			Permission permission = permissionService.selectByPrimaryKey(permissionId);
			m.addAttribute("permission", permission);
		}
		
		
		//所有type=menu 菜单权限才有子权限，所以查询出所有的 menu=type的权限作为父权限
		PermissionExample example = new PermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andTypeEqualTo("menu");
		List<Permission> parents = permissionService.selectByExample(example );
		m.addAttribute("parents", parents);

		return "permissionEdit";
	}



}
